package com.ruoyi.projects.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.projects.Vos.TimeArr;
import com.ruoyi.projects.Vos.slApointVo;
import com.ruoyi.projects.domain.SlProduct;
import com.ruoyi.projects.service.ISlProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.projects.domain.SlApointForm;
import com.ruoyi.projects.service.ISlApointFormService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预约排期Controller
 * 
 * @author yangz
 * @date 2024-10-23
 */
@RestController
@RequestMapping("/projects/SlApointForm")
public class SlApointFormController extends BaseController
{
    @Autowired
    private ISlApointFormService slApointFormService;
    @Autowired
    private ISlProductService productService;


    @PostMapping("/getApointFormInfo")
    public AjaxResult getApointFormInfo(@RequestBody SlApointForm slApointForm)
    {
        HashMap<Object, Object> resultMap = new HashMap<>();
        List<slApointVo> slApointVos = slApointFormService.querySlApointVo(slApointForm);
        if (slApointVos==null||slApointVos.size()==0){
            resultMap.put("appointments",new ArrayList<>());
            resultMap.put("timeSlots",slApointFormService.getApointFormInfo(null));
        }else {
            resultMap.put("appointments",slApointVos);
            resultMap.put("timeSlots",slApointFormService.getApointFormInfo(slApointVos));
        }
        return AjaxResult.success(resultMap);
    }

    /**
     * 查询预约排期列表
     */
    @PreAuthorize("@ss.hasPermi('projects:SlApointForm:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlApointForm slApointForm)
    {
        startPage();
        List<SlApointForm> list = slApointFormService.selectSlApointFormList(slApointForm);
        list.forEach(item->{
            item.setProductNames(productService.queryProductNames(item.getProductId()));
        });
        return getDataTable(list);
    }

    /**
     * 导出预约排期列表
     */
    @PreAuthorize("@ss.hasPermi('projects:SlApointForm:export')")
    @Log(title = "预约排期", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlApointForm slApointForm)
    {
        List<SlApointForm> list = slApointFormService.selectSlApointFormList(slApointForm);
        ExcelUtil<SlApointForm> util = new ExcelUtil<SlApointForm>(SlApointForm.class);
        util.exportExcel(response, list, "预约排期数据");
    }

    /**
     * 获取预约排期详细信息
     */
    @PreAuthorize("@ss.hasPermi('projects:SlApointForm:query')")
    @GetMapping(value = "/{apointId}")
    public AjaxResult getInfo(@PathVariable("apointId") Long apointId)
    {
        return AjaxResult.success(slApointFormService.selectSlApointFormByApointId(apointId));
    }

    /**
     * 新增预约排期
     */
    @PreAuthorize("@ss.hasPermi('projects:SlApointForm:add')")
    @Log(title = "预约排期", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlApointForm slApointForm)
    {
        slApointForm.setTimes(1);
        return toAjax(slApointFormService.insertSlApointForm(slApointForm));
    }

    /**
     * 修改预约排期
     */
    @PreAuthorize("@ss.hasPermi('projects:SlApointForm:edit')")
    @Log(title = "预约排期", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlApointForm slApointForm)
    {
        return toAjax(slApointFormService.updateSlApointForm(slApointForm));
    }

    /**
     * 删除预约排期
     */
    @PreAuthorize("@ss.hasPermi('projects:SlApointForm:remove')")
    @Log(title = "预约排期", businessType = BusinessType.DELETE)
	@DeleteMapping("/{apointIds}")
    public AjaxResult remove(@PathVariable Long[] apointIds)
    {
        return toAjax(slApointFormService.deleteSlApointFormByApointIds(apointIds));
    }
}
